*****This program reads in the time-use data*******

**Set root directory
local rootdir
cd "`rootdir'TUS"


foreach i in R U{


****Level 1*******
clear
set mem 1g
infix str blank 1-10 state 11 district 12-13 town 14-16 sector 17 serialno 18-21 hamlet 22-24 sstratum 25 subround 26 hhno 27-28 level 29-30 str blank2 31-35 relinf 36 scode 37 sub 38 hhsize 39-41 str nic 42-44 str nco 45-47 htype 48 religion 49 hgroup 50 homestead 51 land 52-58 landpos 59-65 avgexp 66-71 mpce 72-77 struct 78 disabled 79-80 str update 81 stratum 82 distclass 83 subsample 84 ssmultdist 85-96 multdist 97-108 ssmultstate 109-120 multstate 121-132 lit 133 litf 134 emp 135 empf 136 sexhh 137 str blank4 138-152 using D152`i'.txt
keep if level==1
replace land=land/100
replace landpos=landpos/100
replace ssmultdist=ssmultdist/100
replace multdist=multdist/100
replace ssmultstate=ssmultstate/100
replace multstate=multstate/100
drop blank* subround
save level1`i', replace


*******Level 2********
clear
infix str blank 1-10 state 11 district 12-13 town 14-16 sector 17 serialno 18-21 hamlet 22-24 sstratum 25 subround 26 hhno 27-28 level 29-30 persno 31-32 str blank2 33-35 relhh 36 sex 37 age 38-39 mstat 40 disabled 41 education 42-43 mainstatus 44-45 mainenterprise 46 str mainindustry 47-49 secstatus 50-51 secenterprise 52 str secindustry 53-55 decisionmaking 56 daysnormal 57 daysvariant 58 daysabnormal 59 str blank4 60-80 update 81 str blank5 82-152 using D152`i'.txt
keep if level==2
drop blank* subround
save level2`i', replace

*******Level 3********
clear
infix str blank 1-10 state 11 district 12-13 town 14-16 sector 17 serialno 18-21 hamlet 22-24 sstratum 25 subround 26 hhno 27-28 level 29-30 persno 31-32 str blank2 33-37 normaldate 38-43 normalday 44 variantdate 45-50 variantday 51 abnormaldate 52-57 abnormalday 58 str blank3 59-152 using D152`i'.txt
keep if level==3
drop blank* 
save level3`i', replace

*******Level 4********
clear
infix str blank 1-10 state 11 district 12-13 town 14-16 sector 17 serialno 18-21 hamlet 22-24 sstratum 25 subround 26 hhno 27-28 level 29-30 persno 31-32 age 33-34 daytype 35 hourno 36-37 actno 38 hourfrom 39-40 hourto 41-42 multact 43 acttime 44-45 withinhh 46 actcode 47-49 modepay 50 str blank3 51-81 stratum 82 distclass 83 subsample 84 ssmultdist 85-96 multdist 97-108 ssmultstate 109-120 multstate 121-132 hgroup 133 religion 134 sex 135 mstatus 136 education 137-138 mpce 139-144 days 145 mainstatus 146-147 hhsize 148-150 filler 151-152 using D152`i'.txt

keep if level==4
drop blank* subround
save level4`i', replace
}


use level1R, clear
append using level1U
bysort sector state serialno sstratum hhno: gen num=_N
save level1, replace

use level2R, clear
append using level2U
duplicates drop sector state serialno sstratum hhno persno, force
save level2, replace

use level3R, clear
append using level3U
gen date=.
replace date=normaldate if normaldate~=0
replace date=variantdate if date==. & variantdate~=0
replace date=abnormaldate if date==. & abnormaldate~=0
tostring date, gen(stdate)  format(%06.0f)
gen year=substr(stdate,5,2)
gen month=substr(stdate,3,2)
replace date=. if year~="98" & year~="99"
destring month, gen(nummonth) force
destring year, gen(numyear) force
replace date=. if nummonth>12 | nummonth<1
summ date state

bysort sector state serialno sstratum hhno: egen mindate=min(date)
replace date=mindate if date==.
summ date state
drop mindate
bysort sector state serialno: egen mindate=min(date)
replace date=mindate if date==.
summ date state
drop mindate
bysort state serialno: egen mindate=min(date)
replace date=mindate if date==.
summ date state
drop mindate

drop year month stdate
tostring date, gen(stdate)  format(%06.0f)
gen year=substr(stdate,5,2)
gen month=substr(stdate,3,2)

drop nummonth numyear
destring month, replace 
destring year, replace

replace year=99 if month<7
replace year=98 if month>6
replace subround=1 if year==98 & month<10
replace subround=2 if year==98 & month>9
replace subround=3 if year==99 & month<4
replace subround=4 if year==99 & month>3
summ state subround

bysort sector state serialno sstratum hhno persno: gen num=_N
duplicates drop sector state serialno sstratum hhno, force
keep sector state serialno sstratum hhno subround month year
save level3, replace

**********This section checks total time use, makes sure it adds up*******************
***Total time per day should add up to 60 minutes * 24 hours = 1440 minutes

use level4R, clear
append using level4U

*18591 households in level1 file
egen hhid=group(sector state serialno sstratum hhno)
summ hhid
**these are unique identifiers

bysort hhid persno daytype: egen tottime=total(acttime)
tab tottime

*222 observations with total time of 2880, doubled up
*issue seems to be that some days are mislabbeled 
*manually fix problems -- only four guys causing the problems
drop if hhid==15843 & persno==2 & daytype==1 & hourfrom==3 & hourto==4 & hourno>1 & hourno<25
drop if hhid==16227 & persno==5 & daytype==2 & hourfrom==3 & hourto==4 & hourno>1 & hourno<25
drop if hhid==17727 & persno==3 & daytype==2 & hourfrom==2 & hourto==3 & hourno>0 & hourno<24
drop if hhid==18579 & persno==2 & daytype==2 & hourfrom==3 & hourto==4 & hourno>1 & hourno<25 & acttime==60
drop tottime
bysort hhid persno daytype: egen tottime=total(acttime)
tab tottime
drop tottime

*check that each person has 7 reported days
bysort sector state serialno sstratum hhno persno daytype: gen counter=_n
replace counter=0 if counter~=1
bysort sector state serialno sstratum hhno persno: egen totday=total(days*counter)
tab totday
drop totday
*confirmed

save level4, replace




